#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/12/18 22:11
# @Author  : lizhen
# @Site    : 
# @File    : cFactory.py
# @Software: PyCharm


class A:
    def __init__(self):
        pass

    def do(self):
        pass


class B:
    def __init__(self):
        pass

    def do(self):
        pass


class FactoryDemo:
    def connection_factory(self, resource):
        if resource == 'A':
            connector = A()
        elif resource == 'B':
            connector = B()
        else:
            raise ValueError('cannot connect to {}'.format(resource))
        return connector

    def connect_to(self, resource):
        factory = None
        try:
            factory = self.connection_factory(resource)
        except ValueError as ve:
            print(ve)
        finally:
            return factory

    def _test_run(self):
        """
        fd = FactoryDemo()
        fd._test_run()
        """
        a = self.connect_to('A')
        a.do()
        b = self.connect_to('B')
        b.do()


if __name__ == '__main__':
    fd = FactoryDemo()
    fd._test_run()
